<?php

/**
 * -----------| 控制器:访问授权 |-----------
 * @category Betterlife
 * @package web.admin.action
 * @author skygreen skygreen2001@gmail.com
 */
class Action_Auth extends ActionReport
{
    /**
     * 退出
     */
    public function logout()
    {
        HttpSession::remove("user_id");
        $this->redirect("auth", "login");
    }

    /**
     * 登录
     */
    public function login()
    {
        $this->view->set("message", "");
        if (HttpSession::isHave('user_id')) {
            $this->redirect("index", "index");
        } elseif (!empty($_POST)) {
            $user     = $this->model->Admin;
            $userdata = Admin::getOne(
                array(
                                "username" => $user->username,
                                "password" => $user->getPassword()
                            )
            );
            LogMe::log($userdata);
            if (empty($userdata)) {
                $this->view->set("message", "用户名或者密码错误");
            } else {
                HttpSession::set('user_id', $userdata->admin_id);
                HttpSession::set('username', $user->username);
                $this->redirect("index", "index");
            }
        }
    }

    /**
     * 注册
     */
    public function register()
    {
        $this->view->set("message", "");
        if (!empty($_POST)) {
            $user     = $this->model->Admin;
            $userdata = Admin::get(array("username" => $user->username));
            if (empty($userdata)) {
                // $pass = $user->getPassword();
                $user->setPassword(md5($user->getPassword()));
                $user->loginTimes = 0;
                $user->save();
                HttpSession::set('user_id', $user->admin_id);
                $this->redirect("index", "index");
            } else {
                $this->view->color = "red";
                $this->view->set("message", "该用户名已有用户注册!");
            }
        }
    }
}
